package com.snnsh.kjb.lottery.dao;

import com.snnsh.kjb.lottery.po.ProductPO;
import com.snnsh.kjb.lottery.util.Dom4jXMLManager;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * packageName com.snnsh.kjb.lottery.dao
 * 通过xml操作dao
 * @author wick
 * @version V1.0.0
 * @date 2025/5/27
 */
@Component
public class ProductByXmlDao implements IProductDao{


    private static final String XML_FILENAME ="Products.xml";


    @Override
    public void addProduct(ProductPO product) {
        //找到之前的
        List<ProductPO> productFromXML = Dom4jXMLManager.getFromXML(XML_FILENAME,ProductPO.class);
        if(CollectionUtils.isEmpty(productFromXML)){
            product.setId(1);
        }
        else {
            ProductPO lastProd = productFromXML.get(productFromXML.size()-1);
            product.setId(lastProd.getId()+1);
        }
        productFromXML.add(product);
        Dom4jXMLManager.write2Xml(productFromXML,ProductPO.class,XML_FILENAME);
    }

    @Override
    public void deleteProduct(Integer id) {
        List<ProductPO> productFromXML = Dom4jXMLManager.getFromXML(XML_FILENAME,ProductPO.class);
        Iterator<ProductPO> iterator = productFromXML.iterator();
        while (iterator.hasNext()){
            ProductPO next = iterator.next();
            if(next.getId().equals(id)){
                iterator.remove();
            }
        }
        Dom4jXMLManager.write2Xml(productFromXML,ProductPO.class,XML_FILENAME);
    }

    @Override
    public List<ProductPO> list(Map<String, Object> params) {
        List<ProductPO> productFromXML = Dom4jXMLManager.getFromXML(XML_FILENAME,ProductPO.class);
        if(CollectionUtils.isEmpty(params)) {
            return productFromXML;
        }
        if(params.containsKey("lotteryFlag") && params.get("lotteryFlag").equals(Boolean.TRUE)) {
            productFromXML = productFromXML.stream().filter(v->{
                if(v.getLeftAmount() > 0 && v.getWeight() > 0){
                    return true;
                }
                else {
                    return false;
                }
            }).collect(Collectors.toList());
        }
        if(params.containsKey("id")){
            Object id = params.get("id");
            productFromXML = productFromXML.stream().filter(v->{
                if(v.getId().equals(id)){
                    return true;
                }
                else {
                    return false;
                }
            }).collect(Collectors.toList());
        }
        return productFromXML;
    }

    @Override
    public void updateProduct(ProductPO product) {
        //找到原来的再更新
        List<ProductPO> productFromXML = Dom4jXMLManager.getFromXML(XML_FILENAME,ProductPO.class);
        Iterator<ProductPO> iterator = productFromXML.iterator();
        while (iterator.hasNext()){
            ProductPO next = iterator.next();
            if(next.getId().equals(product.getId())){
                BeanUtils.copyProperties(product,next,new String[]{});
            }
        }
        Dom4jXMLManager.write2Xml(productFromXML,ProductPO.class,XML_FILENAME);
    }

    public static void main(String[] args) {
        ProductByXmlDao productByXmlDao = new ProductByXmlDao();
        productByXmlDao.addProduct(new ProductPO("小米手环",22F,1));
        List<ProductPO> productFromXML = Dom4jXMLManager.getFromXML(productByXmlDao.XML_FILENAME,ProductPO.class);
        System.out.println(productFromXML);
        int id = 1;
        //System.out.println("--删除id=" + id);
        //productByXmlDao.deleteProduct(id);
        //productFromXML = Dom4jXMLManager.getProductFromXML(productByXmlDao.XML_FILENAME);
        //System.out.println(productFromXML);
        System.out.println("更新Id ：" + id);
        //更新
        productByXmlDao.updateProduct(new ProductPO(id,"小米手机",30F,1));

        productFromXML = Dom4jXMLManager.getFromXML(productByXmlDao.XML_FILENAME,ProductPO.class);
        System.out.println(productFromXML);
    }

    @Override
    public ProductPO findById(Integer id) {
        Map<String,Object> params = new HashMap<>();
        params.put("id",id);
        List<ProductPO> list = list(params);
        if(CollectionUtils.isEmpty(list)){
            return null;
        }
        return list.get(0);
    }
}
